package com.koron.order.linepatrol.satellite.excel;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.util.ListUtils;
import com.koron.order.linepatrol.satellite.bean.convertor.LinePatrolSatelliteConvertor;
import com.koron.order.linepatrol.satellite.bean.excel.LinePatrolSatelliteExcel;
import com.koron.order.linepatrol.satellite.bean.po.LinePatrolSatellite;
import com.koron.order.linepatrol.satellite.service.LinePatrolSatelliteService;
import com.koron.order.overhaul.bean.dto.Equipment;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;

import java.util.List;

/**
 * @author jzp
 * @since 2023/2/8
 */
@Slf4j
@AllArgsConstructor
public class LinePatrolSatelliteExcelListener extends LineAnalysisEventListener<LinePatrolSatelliteExcel> {

    private final LinePatrolSatelliteService linePatrolSatelliteService;
    private final LinePatrolSatelliteConvertor linePatrolSatelliteConvertor;

    // 每隔100条，然后清理list ，方便内存回收
    private static final int BATCH_COUNT = 100;
    // 要保存的数据
    private final List<LinePatrolSatellite> linePatrolSatelliteList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(LinePatrolSatelliteExcel data, AnalysisContext context) {
        LinePatrolSatellite linePatrolSatellite = fillData(data);
        if (linePatrolSatellite == null) {
            return;
        }
        linePatrolSatelliteList.add(linePatrolSatellite);
        if (linePatrolSatelliteList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            linePatrolSatelliteList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    private void saveData() {
        ADOConnection.runTask(getCurrDs(), linePatrolSatelliteService, "saveBatch", Boolean.class, linePatrolSatelliteList);
        log.info("存储数据库成功！");
    }

    private LinePatrolSatellite fillData(LinePatrolSatelliteExcel data) {
        if (data == null) {
            return null;
        }
        LinePatrolSatellite linePatrolSatellite = linePatrolSatelliteConvertor.excelToEntity(data);

        // riskParentType默认高压线路
        linePatrolSatellite.setRiskParentType("高压线路");
        // 导入巡视记录时，初始核实状态为待核实
        linePatrolSatellite.setCheckStatus("1");

        String lineName = linePatrolSatellite.getEquipmentName();
        if(lineName == null){
            fillError(data,0,"线路名称不能为空");
        }
        Equipment line = getLineByName(linePatrolSatellite.getEquipmentName());
        if (line == null) {
            fillError(data, 0, "线路：" + linePatrolSatellite.getEquipmentName() + "不存在");
            return null;
        }  else {
            linePatrolSatellite.setEquipmentId(line.getId());
        }

        // 杆塔
        if (StrUtil.isEmpty(linePatrolSatellite.getTowerName())) {
            fillError(data, 1, "杆塔不能为空");
            return null;
        }
        Equipment tower = getTowerByNameAndChildren(line.getName(), linePatrolSatellite.getTowerName(), line.getChildren());
        if (tower == null) {
            fillError(data, 1, "杆塔：" + data.getTowerNumber() + "不存在");
            return null;
        }  else {
            linePatrolSatellite.setTowerId(tower.getId());
        }

        //数字校验
        boolean number0 = NumberUtil.isNumber(data.getClosestDistanceLine());
        boolean number1 = NumberUtil.isNumber(data.getClosestDistanceTower());
        boolean number2 = NumberUtil.isNumber(data.getLongitude());
        boolean number3 = NumberUtil.isNumber(data.getLatitude());
        if(!number0){
            fillError(data,3,"线路最近距离输入非数值型，不是整数或小数");
            return null;
        }
        linePatrolSatellite.setEquipmentDistance(Integer.valueOf(data.getClosestDistanceLine()));

        if(!number1){
            fillError(data,4,"杆塔最近距离输入非数值型，不是整数或小数");
            return null;
        }
        linePatrolSatellite.setTowerDistance(Integer.valueOf(data.getClosestDistanceTower()));

        if(!number2){
            fillError(data,7,"经度输入非数值型，不是整数或小数");
            return null;
        }

        if(!number3){
            fillError(data,8,"纬度输入非数值型，不是整数或小数");
            return null;
        }

        //风险子类和风险关键字校验
        if(linePatrolSatellite.getRiskType()== null)
        {
            fillError(data,5,"风险子类不能为空");
            return null;
        }

        if (data.getRiskKeyword() == null) {
            fillError(data, 6, "风险关键字不能为空");
            return null;
        }
        return linePatrolSatellite;
    }

}
